package sd.czw.com.mmgapp.utils;

import java.text.SimpleDateFormat;
import java.util.Date;

import sd.czw.com.mmgapp.Contants;

/**
 * Created by czw
 * desc 描述：时间和时间戳的转换
 * on 2017/7/5.
 */
public class SetDataUtil {

    public static String getTime(long l){
        String str = String.valueOf(l);
        return str.substring(0, 10);
    }

    /**
     *
     * @param user_time
     *            时间
     * @return 返回时间戳
     */
    public static String getTime(String user_time) {
        String re_time = null;
        String str=null;
        SimpleDateFormat sdf = new SimpleDateFormat(Contants.DATEFORMAT);
        Date d;
        try {
            d = sdf.parse(user_time);
            long l = d.getTime();
            str = String.valueOf(l);
            re_time = str.substring(0, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return re_time;
        //return str;
    }

    public static String timeStamp2Date(Date data,String format) {
        return timeStamp2Date(String.valueOf(data.getTime()).substring(0,10),format);
    }

    /*
   * 时间戳转换成日期格式字符串
   * @param seconds 精确到秒的字符串
   * @param formatStr
   * @return
           */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()) format = "yyyy-MM-dd HH:mm:ss";
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }

}
